﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.CRM;

using Jzy.Core.Common.Web;
using Jzy.Model.CRM;
using Jzy.Core.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Web;
using Frame.Lib.Core.Dict;
using Frame.Lib.Enums;


namespace Jzy.Web.CRM.Visit
{
    public partial class List : PageList
    {
        private CustomerVisitBO _visit;
        private CustomerVisitBO Visit
        {
            get
            {
                if (_visit == null)
                {
                    _visit = new CustomerVisitBO();
                }
                return _visit;
            }

        }

        private string CustomerID
        {
            get
            {
                return Request["CustomerID"];
            }
        }

        private PagerSortSetting _setting;
        protected void Page_Load(object sender, EventArgs e)
        {
            _setting = this.Footer1.InitPager(new BindViewHandler(BindView));

            if (!IsPostBack)
            {
                Pager.BindDrp(drpPager);
                BindView();
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
            JSUtil.SetTableHeight("list", 145);
        }


        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "联系记录");
            
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ShowForm(lnkAdd, "增加联系人", null, "Edit.aspx?CustomerID=" + this.CustomerID, 480, 220);

        }

        protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindView();
        }

        private void BindView()
        {

            LVUtil.Init(lv, lvHeader);

            IEnumerable<CustomerVisitInfo> list = this.Visit.GetList(int.Parse(this.CustomerID));

            Pager.SetPageSize(_setting, drpPager);

            list = _setting.GetList<CustomerVisitInfo>(list);

            DictBO dict = new DictBO();

            dict.SetName<CustomerVisitInfo>(list, DictItem.联系方式, "ContactMethodName", "ContactMethod");

            lblStatistic.Text = "[&nbsp;共<b style='color:red;'> " + _setting.RecordCount + "</b>&nbsp;条联系记录&nbsp;]";

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataBind();


        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }

            //要删除相关的图片和附件
            this.Visit.LogicDelete(strSelectIDs);
            BindView();
        }



        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                string strID = StrUtil.EncryptUrl(lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString());
                HyperLink lnkEdit = (HyperLink)e.Item.FindControl("lnkEdit");
                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
                string url = "Edit.aspx?ID=" + strID;
                JSUtil.ShowForm(lnkEdit, "增加联系人", null, url, 480, 220);
                JSUtil.ShowForm(lnkName, "增加联系人", null, url, 480, 220);
            }
        }


    }
}
